package com.xilihui.xlh.business.activitys;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.text.TextUtils;
import android.view.View;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.TextView;

import com.xilihui.xlh.R;
import com.xilihui.xlh.business.requests.LoginRequest;
import com.xilihui.xlh.constant.StrConst;
import com.xilihui.xlh.core.app.BaseCompatActivity;
import com.xilihui.xlh.core.app.BaseEntity;
import com.xilihui.xlh.core.http.DoTransform;
import com.xilihui.xlh.core.http.HttpSubscriber;
import com.xilihui.xlh.core.util.CountDown;
import com.xilihui.xlh.core.util.LogUtil;
import com.xilihui.xlh.core.util.SPUtil;
import com.xilihui.xlh.core.util.TextExtend;
import com.xilihui.xlh.core.util.ToastUtil;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * Created by Administrator on 2018/5/28.
 */

public class RegisterActivity extends BaseCompatActivity implements View.OnClickListener {
    
    @BindView(R.id.et_phone)
    EditText et_phone;
    @BindView(R.id.et_code)
    EditText et_code;
    @BindView(R.id.tv_sendCode)
    TextView tv_sendCode;
    @BindView(R.id.et_passwd)
    EditText et_passwd;
    @BindView(R.id.et_surePasswd)
    EditText et_surePasswd;
    @BindView(R.id.checkbox)
    CheckBox checkBox;
    @BindView(R.id.tv_login)
    TextView tv_reg;
    @BindView(R.id.tv_protocol)
    TextView tv_protocol;
    
    private CountDown countDown;
    private long time = 0L;
    private String phone = "";
    private String code = "";
    private String passwd = "";
    private String surePasswd = "";
    
    @OnClick(R.id.iv_close)
    public void close(){
        finish();
    }
    
    @Override
    public int getLayout() {
        return R.layout.activity_register;
    }

    @Override
    public String initTitle() {
        return null;
    }

    @Override
    public void initUI(@Nullable Bundle savedInstanceState) {
        tv_sendCode.setOnClickListener(this);
        tv_reg.setOnClickListener(this);
        tv_protocol.setOnClickListener(this);
        initcountDown();
    }

    @Override
    public void initData() {

    }

    /**
     * 注册
     */
    public void reg(){
        phone = et_phone.getText().toString().trim();
        code = et_code.getText().toString().trim();
        passwd = et_passwd.getText().toString().trim();
        surePasswd = et_surePasswd.getText().toString().trim();
        
        if(TextUtils.isEmpty(phone)){
            ToastUtil.toastShortNegative("请输入手机号码");
            return;
        }
        
        if(!TextExtend.isMobile(phone)){
            ToastUtil.toastShortNegative("请输入正确的手机号码");
            return;
        }
        
        if(TextUtils.isEmpty(code)){
            ToastUtil.toastShortNegative("请输入验证码");
            return;
        }
        
        if(TextUtils.isEmpty(passwd)){
            ToastUtil.toastShortNegative("请输入密码");
            return;
        }
        
        if(passwd.length()<6||passwd.length()>16){
            ToastUtil.toastShortNegative("请输入6-20位密码");
            return;
        }
        
        if(TextUtils.isEmpty(surePasswd)){
            ToastUtil.toastShortNegative("请输入确认密码");
            return;
        }
        
        if(surePasswd.length()<6||surePasswd.length()>16){
            ToastUtil.toastShortNegative("请输入6-20位确认密码");
            return;
        }
        
        if(!passwd.equals(surePasswd)){
            ToastUtil.toastShortNegative("两次密码不一致");
            return;
        }
        
        if(!checkBox.isChecked()){
            ToastUtil.toastShortNegative("请同意千锐集助手服务协议");
            return;
        }
        
        LoginRequest.reg(phone,passwd,surePasswd,code,StrConst.app_key)
                .compose(DoTransform.<BaseEntity>applyScheduler())
                .compose(RegisterActivity.this.<BaseEntity>bindToLifecycle())
                .subscribe(new HttpSubscriber<BaseEntity>(RegisterActivity.this) {
                    @Override
                    protected void onSuccess(BaseEntity entity) {
                        ToastUtil.toastShortPositive(entity.getMsg());
                        finish();
                    }

                    @Override
                    protected void onFail(String msg) {
                        ToastUtil.toastShortNegative(msg);
                    }
                });
        
    }
    
    public void sendCode(){
        
        String phone = et_phone.getText().toString().trim();
        
        if(TextUtils.isEmpty(phone)){
            ToastUtil.toastShortNegative("请输入手机号码");
            return;
        }
        
        if(!TextExtend.isMobile(phone)){
            ToastUtil.toastShortNegative("请输入正确的手机号码");
            return;
        }
        
        tv_sendCode.setEnabled(false);
        
        LoginRequest.sendCode(phone, StrConst.scene_zc)
                .compose(DoTransform.<BaseEntity>applyScheduler())
                .compose(RegisterActivity.this.<BaseEntity>bindToLifecycle())
                .subscribe(new HttpSubscriber<BaseEntity>(RegisterActivity.this) {
                    @Override
                    protected void onSuccess(BaseEntity entity) {
                        tv_sendCode.setEnabled(true);
                        ToastUtil.toastShortPositive(entity.getMsg());
                        countDown.execute((TextView) tv_sendCode, 60, new CountDown.Callback() {
                            @Override
                            public void onStart() {
                                tv_sendCode.setSelected(false);
                            }

                            @Override
                            public void onCounting(TextView tv, long second, String text) {
                                SPUtil.put(RegisterActivity.this,"time",System.currentTimeMillis()+second*1000);
                                LogUtil.i("mylog","set time:"+System.currentTimeMillis());
                            }

                            @Override
                            public void onFinish() {
                                tv_sendCode.setSelected(true);
                            }
                        });
                    }

                    @Override
                    protected void onFail(String msg) {
                        tv_sendCode.setEnabled(true);
                        ToastUtil.toastShortNegative(msg);
                    }
                });
        
    }

    public void initcountDown(){

        countDown = new CountDown();
        countDown.setTextOnCounting("", "重新发送");
        countDown.setTextOnFinished("发送验证码");
        countDown.setTv(tv_sendCode);

        time = (long) SPUtil.get(this,"time",0L);
        long curTime = System.currentTimeMillis();
        long residueTime = time-curTime;
        LogUtil.i("mylog","time:"+time+"\ncurTime:"+curTime+"\nresidueTime"+residueTime);
        long duration = residueTime/1000;
        LogUtil.i("mylog","duration:"+duration);
        if(duration>0){
            LogUtil.i("mylog","执行");
            countDown.execute((TextView) tv_sendCode, duration, new CountDown.Callback() {
                @Override
                public void onStart() {
                    tv_sendCode.setSelected(false);
                }

                @Override
                public void onCounting(TextView tv, long second, String text) {
                    SPUtil.put(RegisterActivity.this,"time",System.currentTimeMillis()+second*1000);
                    LogUtil.i("mylog","set time:"+System.currentTimeMillis());
                }

                @Override
                public void onFinish() {
                    tv_sendCode.setSelected(true);
                }
            });
        }
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()){
            case R.id.tv_sendCode:
                sendCode();        
                break;
            case R.id.tv_login:
                reg();
                break;
            case R.id.tv_protocol:
                Intent intent = new Intent(RegisterActivity.this,WebActivity.class);
                intent.putExtra("type",1);
                startActivity(intent);
                break;
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if(countDown!=null){
            countDown.cancel();
        }
    }
    
}
